﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.SanXuat.UTI;
using ThaiHoa.Win.QLSX.DAL;

namespace ThaiHoa.Win.QLSX.SanXuat.UI
{
    public partial class frmCongNhanSelectView : Form
    {
        #region Fields
        KhoDBContext db;
        Access acs;
        List<CongNhan> listCongNhan;
        public event EventHandler<SelectCongNhanEventArgs> SelectedCongNhan;
        #endregion
        #region Constructor
        public frmCongNhanSelectView()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void KhoiTao()
        {
            db = new KhoDBContext();
            acs = new Access();
            listCongNhan = db.CongNhan.Include("LoaiCongNhan").Where(c => c.TrangThai).ToList();
            dgvCongNhan.DataSource = listCongNhan;
            CustomerGridView();
        }
        private void CustomerGridView()
        {
            //Ẩn toàn bộ columns
            foreach (DataGridViewColumn item in dgvCongNhan.Columns)
            {
                item.Visible = false;
            }
            //Tùy chỉnh lại
            //Không cho edit
            dgvCongNhan.ReadOnly = true;
            //Không cho thêm mới
            dgvCongNhan.AllowUserToAddRows = false;
            //Ẩn row header
            dgvCongNhan.RowHeadersVisible = false;
            //Tên
            var c = dgvCongNhan.Columns["HoVaTen"];
            c.Visible = true;
            c.Width = 200;
            c.DisplayIndex = 1;

            //Chức vụ
            c = dgvCongNhan.Columns["LoaiCongNhan"];
            c.Visible = true;
            c.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            c.DisplayIndex = 2;

        }
        private void ChonCongNhan()
        {
            if ((Guid)dgvCongNhan.CurrentRow.Cells["Id"].Value == null) return;
            Guid Id = (Guid)dgvCongNhan.CurrentRow.Cells["Id"].Value;
            if (Id == null || Id == Guid.Empty) return;
            CongNhan item = db.CongNhan.Where(c => c.Id.Equals(Id) && c.TrangThai).FirstOrDefault();
            if (item == null) return;
            if (SelectedCongNhan != null)
            {
                SelectedCongNhan(this, new SelectCongNhanEventArgs(item));
                this.Close();
            }
        }
        #endregion
        #region Events
        private void frmCongNhanSelectView_Load(object sender, EventArgs e)
        {
            KhoiTao();
        }
        private void dgvCongNhan_DoubleClick(object sender, EventArgs e)
        {
            ChonCongNhan();
        }
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmCongNhanDetail _frmCongNhanDetail = new frmCongNhanDetail();
            _frmCongNhanDetail.DBChanged += new EventHandler(_frmCongNhanDetail_DBChanged);
            acs.ShowFormCongNhanDetail(_frmCongNhanDetail, Guid.Empty);
        }

        void _frmCongNhanDetail_DBChanged(object sender, EventArgs e)
        {
            //load lại dữ liệu
            listCongNhan = db.CongNhan.Include("LoaiCongNhan").Where(c => c.TrangThai).ToList();
            dgvCongNhan.DataSource = listCongNhan;
            CustomerGridView();
        }
        #endregion

        

        
    }

    public class SelectCongNhanEventArgs : EventArgs
    {
        public CongNhan CongNhan;

        public SelectCongNhanEventArgs(CongNhan congNhan)
        {
            CongNhan = congNhan;
        }
    }
}
